package hauwei.year2024.month4;

import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

public class CloudCount {
    static class Log{
        String time;
        String countFlag;
        int timeLong;
        public Log(String time, String countFlag, int timeLong) {
            this.time = time;
            this.countFlag = countFlag;
            this.timeLong = timeLong;
        }

        @Override
        public boolean equals(Object o) {
            if (o == null || getClass() != o.getClass()) return false;
            Log log = (Log) o;
            return timeLong == log.timeLong ;
        }

        @Override
        public int hashCode() {
            return Objects.hash(time, countFlag, timeLong);
        }
    }
    static class Customer{
        String name;
        List<Log> logs;
        public Customer(String s) {
            name =s;
            logs = new ArrayList<Log>();
        }

        public void addLog(Log log) {
            if(!logs.contains(log)) {
                logs.add(log);
            }
        }
        public List<Log> getLogs() {
            return logs;
        }

        @Override
        public boolean equals(Object obj) {
            return name.equals(((Customer)obj).name);
        }
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int logNum = Integer.parseInt(sc.nextLine());
        TreeSet<Customer> customers = new TreeSet<>(
                (a,b)->{
                   return a.name.compareTo(b.name);
                }
        );
        for (int i = 0; i < logNum; i++) {
            String str=sc.nextLine();
            System.out.println(str);
            String[] split = str.split(",");
            String time = split[0];
            String name = split[1];
            String countFlag = split[2];
            int timeLong = Integer.parseInt(split[3]);
            Log log=new Log(time,countFlag,timeLong);
            Iterator<Customer> iterator = customers.iterator();
            boolean found = false;
            while(iterator.hasNext()){//如果有 ;
                Customer cus=iterator.next();
                if(cus.name.equals(name)){
                    cus.addLog(log);
                }
            }
            //如果没有
            if(!found){
                Customer customer = new Customer(name);
                customer.addLog(log);
                customers.add(customer);
            }
        }
        int factor=Integer.parseInt(sc.nextLine());
        HashMap<String,Integer> map=new HashMap<String,Integer>();
        for(int i=0;i<customers.size();i++){
            String fs=sc.nextLine();
            String[] split = fs.split(",");
            map.put(split[0],Integer.parseInt(split[1]));
        }
        customers.forEach(customer -> {
            AtomicInteger sum= new AtomicInteger();
            customer.getLogs().stream().forEach(log -> {
                Integer count=map.get(log.countFlag);
                if(count!=null){
                    sum.addAndGet(count * log.timeLong);
                }
            });
            System.out.println(customer.name+","+sum.toString());
        });
        sc.close();
    }
}
